package com.soxing.braindata.domain.stat;

import lombok.Data;
import lombok.SneakyThrows;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Data
public class YearEntity {
    public static String name = "年龄统计";
    public static String key = "YEAR_BAR";


    static String[] level = {"0-5","6-10","11-15","16-20","21-25","26-30","31-35","36-40","41-45","46-50","51-55","56-60","61-65","66-70","71-"};
    private Map<String,Long> stat;
    public YearEntity(List<Date> siteYear)
    {
        Date date = new Date();
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,Long> record = siteYear.stream().map(
                x->{

                    Date date1 = x;
                    ZoneId zoneId = ZoneId.systemDefault();
                    Instant instant1 = date1.toInstant();
                    LocalDate localDate1 = instant1.atZone(zoneId).toLocalDate();
                    Instant instant = date.toInstant();
                    LocalDate localDate = instant.atZone(zoneId).toLocalDate();

                    Period period = Period.between(localDate1, localDate);
                    return period.getYears();


                }
        ).collect(Collectors.groupingBy(
                x->{
                    if(x>70)
                    {
                        return level[level.length-1];
                    }
                    else if(x<=0)
                    {
                        return level[0];
                    }
                    return level[(int)((x-1)/5)];
                },Collectors.counting()
        ));
        this.stat = new TreeMap<String,Long>();
        this.stat.putAll(record);
    }
}
